/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backingbeans;

import entities.Playlist;
import entities.UUser;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import sessions.PlaylistManager;
import utils.ClientMessage;

/**
 *
 * @author Antoine Boulinguez et Shyn-Yuan Cheng
 */
@ManagedBean(name="myPlaylist")
@ViewScoped
public class MyPlaylists implements Serializable {
    @EJB
    private PlaylistManager playlistManager;
    @Inject
    private UserSession userSession;
    
    private String playlistTitle;
    private String description;
    private boolean publicPlaylist;
    
    private Playlist selectedPlaylist;
    private UUser selectedFriendToShareWith;
    
    private Converter playlistConverter = new Converter() {  
  
        @Override  
        public Object getAsObject(FacesContext context, UIComponent component, String value) {  
            return playlistManager.getPlaylist(Long.valueOf(value));
       }  
        @Override  
        public String getAsString(FacesContext context, UIComponent component, Object value) {  
            if(value == null) {
                return "";
            }
            
            return String.valueOf(((Playlist) value).getId());
        }  
    };

    public String getPlaylistTitle() {
        return playlistTitle;
    }

    public void setPlaylistTitle(String playlistTitle) {
        this.playlistTitle = playlistTitle;
    }
    
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isPublicPlaylist() {
        return publicPlaylist;
    }

    public void setPublicPlaylist(boolean publicPlaylist) {
        this.publicPlaylist = publicPlaylist;
    }

    public Playlist getSelectedPlaylist() {
        return selectedPlaylist;
    }

    public void setSelectedPlaylist(Playlist selectedPlaylist) {
        this.selectedPlaylist = selectedPlaylist;
        this.playlistTitle = selectedPlaylist.getTitle();
        this.description = selectedPlaylist.getDescription();
        this.publicPlaylist = !selectedPlaylist.isPrivatePlaylist();
    }
    
    public Converter getPlaylistConverter() {
        return playlistConverter;
    }

    public void setPlaylistConverter(Converter playlistConverter) {
        this.playlistConverter = playlistConverter;
    }

    public UUser getSelectedFriendToShareWith() {
        return selectedFriendToShareWith;
    }

    public void setSelectedFriendToShareWith(UUser selectedFriendToShareWith) {
        this.selectedFriendToShareWith = selectedFriendToShareWith;
    }
    
    // Handdle the playlist creation form
    public void createPlayList() {
        if(!playlistManager.createPlayList(userSession.getUser(), playlistTitle, description, !publicPlaylist)) {
            ClientMessage.sendClientMessage("Une playlist portant ce titre existe déjà !", FacesMessage.SEVERITY_ERROR);
        }
        else {
            ClientMessage.sendClientMessage("Votre playlist a été créée.", FacesMessage.SEVERITY_INFO);
        }
        
    }
    
    public void editPlaylist() {
        if(playlistManager.editPlaylist(userSession.getUser(), selectedPlaylist, playlistTitle, description, !publicPlaylist)) {
            ClientMessage.sendClientMessage("Informations sur la playlist modifiées !", FacesMessage.SEVERITY_INFO);
        }
        else {
            ClientMessage.sendClientMessage("Une playlist portant ce titre existe déjà !", FacesMessage.SEVERITY_ERROR);
        }
        
    }
    
    public void share() {
        playlistManager.sharePlaylist(selectedPlaylist, userSession.getUser(), selectedFriendToShareWith);
        ClientMessage.sendClientMessage("Playlist partagé !", FacesMessage.SEVERITY_INFO);
    }
    
    // Delete the selected playlist
    public void delete() {
        playlistManager.remove(selectedPlaylist, userSession.getUser());
        ClientMessage.sendClientMessage("Playlist supprimé !", FacesMessage.SEVERITY_INFO);
    }

    public List<Playlist> getPublicPlaylists() {
        return playlistManager.getPublicPlaylists();
    }
    
}
